import type {
  App
} from 'vue'
import type {
  RouteRecordRaw
} from 'vue-router'
import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
import viewRouters from '@/views/router'
const routerHistory = createWebHashHistory()
// createWebHashHistory hash 路由
// createWebHistory history 路由
// createMemoryHistory 带缓存 history 路由
const Layout = () => import('@/layout/index.vue')
export const constantRoute: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue')
  }
]

const basicRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    name: 'Home',
    meta: {
      title: '首页',
      icon: 'el-icon-s-home'
    },
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/home/index.vue')
      },
      ...viewRouters
    ]
  },
  ...constantRoute
]
console.log(basicRoutes, 'routerrouterrouter');

const router = createRouter({
  history: routerHistory,
  routes: basicRoutes
})

// 删除/重置路由
export function resetRoute(): void {
  router.getRoutes().forEach((route) => {
    const {
      name
    } = route
    if (name) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export function setupRouter(app: App<Element>): void {
  app.use(router)
}

export default router